<template>
  <div class="right-content">
    <div class="right-content-preview">
      <div class="wechat-preview">
        <div class="wechat-header">
          <div class="wechat-title">用户消息预览</div>
        </div>
        <div class="wechat-messages" ref="messageContainer">
          <template v-for="message in messages" :key="message.type + message.text">
            <div class="message-item" :class="message.type">
              <div class="message-avatar">
                <img :src="avatarSrc" alt="avatar" />
              </div>
              <div class="message-content">
                <div class="message-bubble">
                  <template v-if="message.type === 'text'">
                    {{ message.text }}
                  </template>
                  <template v-else-if="message.type === 'image'">
                    <img :src="message.imageUrl" class="preview-image" />
                  </template>
                  <template v-else-if="message.type === 'video'">
                    <video :src="message.videoUrl" class="preview-video" controls />
                  </template>
                  <template v-else-if="message.type === 'link'">
                    <div class="link-preview">
                      <div class="link-title">{{ message.linkTitle }}</div>
                      <div class="link-url">{{ message.linkUrl }}</div>
                      <div class="link-desc">{{ message.linkDesc }}</div>
                    </div>
                  </template>
                  <template v-else-if="message.type === 'miniprogram'">
                    <div class="miniprogram-preview">
                      <img v-if="message.miniprogramImage" :src="message.miniprogramImage" class="preview-image" />
                      <div class="miniprogram-title">{{ message.miniprogramTitle }}</div>
                      <div class="miniprogram-path">{{ message.miniprogramPath }}</div>
                    </div>
                  </template>
                </div>
              </div>
            </div>
          </template>
        </div>
      </div>
    </div>
  </div>
</template>

<script lang="ts">
import { defineComponent, ref, watch, nextTick, computed } from 'vue';
import defaultAvatar from '@/assets/avatar.jpg';
import { useUserStore } from '@/store/modules/user';

interface MessageData {
  type: 'text' | 'image' | 'video' | 'link' | 'miniprogram';
  text?: string;
  imageUrl?: string;
  videoUrl?: string;
  linkUrl?: string;
  linkTitle?: string;
  linkDesc?: string;
  miniprogramPath?: string;
  miniprogramTitle?: string;
  miniprogramImage?: string;
}

export default defineComponent({
  name: 'UserMessagePreview',
  props: {
    messages: {
      type: Array as () => MessageData[],
      required: true
    }
  },
  setup(props) {
    const messageContainer = ref<HTMLElement | null>(null);

    // 从userStore获取avatar
    const userStore = useUserStore();
    const avatar = computed(() => userStore.userInfo.avatar);
    const avatarSrc = computed(() => avatar.value || defaultAvatar);

    // 监听消息列表变化，自动滚动到底部
    watch(
      () => props.messages,
      () => {
        nextTick(() => {
          if (messageContainer.value) {
            messageContainer.value.scrollTop = messageContainer.value.scrollHeight;
          }
        });
      },
      { deep: true }
    );

    return {
      messageContainer,
      avatarSrc
    };
  }
});
</script>

<style lang="scss" scoped>
.right-content {
  width: 20%;
  background: #f6f8fa;
  border-radius: 30px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 20px rgba(0, 0, 0, 0.06);
  border: 1px solid #e5e7eb;
  position: relative;
  &::before {
    content: '';
    position: absolute;
    top: 0;
    left: 50%;
    transform: translateX(-50%);
    width: 40%;
    height: 20px;
    background: #e5e7eb;
    border-radius: 0 0 10px 10px;
  }

  .right-content-title {
    padding: 20px;
    font-size: 18px;
    font-weight: 600;
    color: #1f2937;
    border-bottom: 1px solid #e5e7eb;
    background: #ffffff;
  }

  .right-content-preview {
    flex: 1;
    overflow: hidden;
    height: calc(100vh - 400px);

    .wechat-preview {
      background: #ffffff;
      border-radius: 30px;
      height: 100%;
      display: flex;
      flex-direction: column;
      border: 1px solid #eef2f6;

      .wechat-header {
        padding: 30px 16px 16px 16px;
        background: linear-gradient(to right, #f8fafc, #ffffff);
        border-bottom: 1px solid #eef2f6;
        text-align: center;
        border-radius: 30px 30px 0 0;
        .wechat-title {
          font-size: 17px;
          font-weight: 600;
          color: #2c3e50;
          letter-spacing: 0.5px;
        }
      }

      .wechat-messages {
        flex: 1;
        padding: 20px;
        overflow-y: auto;
        background: #f6f8fa;
        background-image: linear-gradient(rgba(0, 0, 0, 0.02) 1px, transparent 1px), linear-gradient(90deg, rgba(0, 0, 0, 0.02) 1px, transparent 1px);
        background-size: 20px 20px;
        border-radius: 0 0 30px 30px;
        .message-item {
          display: flex;
          margin-bottom: 20px;
          align-items: flex-start;
          animation: fadeIn 0.3s ease-in-out;

          @keyframes fadeIn {
            from {
              opacity: 0;
              transform: translateY(10px);
            }
            to {
              opacity: 1;
              transform: translateY(0);
            }
          }

          .message-avatar {
            width: 44px;
            height: 44px;
            border-radius: 8px;
            overflow: hidden;
            margin-right: 12px;
            flex-shrink: 0;
            box-shadow: 0 2px 8px rgba(0, 0, 0, 0.08);
            border: 2px solid #ffffff;

            img {
              width: 100%;
              height: 100%;
              object-fit: cover;
            }
          }

          .message-content {
            max-width: 70%;

            .message-bubble {
              background: #ffffff;
              padding: 14px;
              border-radius: 12px;
              box-shadow: 0 2px 8px rgba(0, 0, 0, 0.04);
              word-break: break-all;
              position: relative;
              border: 1px solid #eef2f6;

              &::before {
                content: '';
                position: absolute;
                left: -8px;
                top: 14px;
                width: 16px;
                height: 16px;
                background: #ffffff;
                transform: rotate(45deg);
                border-left: 1px solid #eef2f6;
                border-bottom: 1px solid #eef2f6;
              }

              .preview-image {
                border-radius: 8px;
              }

              .preview-video {
                border-radius: 8px;
              }

              .link-preview {
                background: #f8fafc;
                border-radius: 8px;
                padding: 12px;
                border: 1px solid #eef2f6;

                .link-title {
                  font-weight: 600;
                  color: #2c3e50;
                  margin-bottom: 6px;
                  font-size: 15px;
                }

                .link-url {
                  color: #3b82f6;
                  font-size: 13px;
                  margin-bottom: 6px;
                  word-break: break-all;
                }

                .link-desc {
                  color: #64748b;
                  font-size: 13px;
                  line-height: 1.5;
                }
              }

              .miniprogram-preview {
                background: #f8fafc;
                border-radius: 8px;
                padding: 12px;
                border: 1px solid #eef2f6;

                .preview-image {
                  width: 100%;
                  height: 140px;
                  object-fit: cover;
                  margin-bottom: 10px;
                  border-radius: 6px;
                }

                .miniprogram-title {
                  font-weight: 600;
                  color: #2c3e50;
                  margin-bottom: 6px;
                  font-size: 15px;
                }

                .miniprogram-path {
                  color: #64748b;
                  font-size: 13px;
                  line-height: 1.5;
                }
              }
            }
          }
        }
      }
    }
  }
}
</style> 